<template>
  <panel title="商品添加">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="商品名称">
        <el-input class="nm" v-model="form.name" placeholder="商品名称"></el-input>
      </el-form-item>

      <el-form-item label="商品分类">
        <el-select v-model="form.category" placeholder="请选择商品分类">
          <el-option
            v-for="cate in categorie"
            :key="cate.cateName"
            :value="cate.cateName"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="商品价格">
        <el-input-number
          v-model="num"
          @change="handleChange"
          :min="0"
          label="描述文字"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="商品图片">
        <el-upload
          class="avatar-uploader"
          action="http://127.0.0.1:5000/goods/goods_img_upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imgUrl" :src="API + imgUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="商品描述">
        <el-input class="ms" type="textarea" v-model="form.desc"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加商品</el-button>
      </el-form-item>
    </el-form>
  </panel>
</template>

<script>
import Panel from "../../components/panel/Panel.vue";
import { categories, add } from "@/api/goods.js";
export default {
  components: {
    Panel,
  },
  data() {
    return {
      API: "http://127.0.0.1:5000",
      imgUrl: "",
      num: 200,
      form: {
        name: "",

        category: "",

        desc: "",
      },
      categorie: [],
    };
  },
  methods: {
    // 获取分类
    async categories() {
      try {
        let res = await categories();

        this.categorie = res.data.categories;
      } catch (err) {
        console.log(err);
      }
    },
    async onSubmit() {
      try {
        let res = await add({
          name: this.form.name,
          category: this.form.category,
          price: this.num,
          imgUrl: this.imgUrl.substr(22),
          goodsDesc: this.form.desc,
        });
          if(res.status===200){
            this.$router.push("/goods/goods-list");
          }
        console.log(res);
      } catch (err) {
        console.log(err);
      }
    },
    handleChange(value) {
      console.log(value);
    },
    handleAvatarSuccess(res) {
      let { code, imgUrl, msg } = res;

      // 上传成功提示
      if (code === 0) {
        console.log(res);
        this.$message({
          type: "success",
          message: msg,
        });

        // 回填
        this.imgUrl = imgUrl;
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  created() {
    this.categories();
  },
};
</script>

<style lang="less" scoped>
.el-form {
  background-color: #fff;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
/deep/.nm {
  width: 400px;
}
/deep/.ms {
  width: 400px;
}
</style>
